package edu.action;



import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.model.Blank;
import edu.model.Form;
import edu.model.Patient;
import edu.model.PatientForm;
import edu.model.PatientFormBlank;
import edu.service.FormService;
import edu.service.PatientFormBlankService;
import edu.service.PatientFormService;
import edu.service.PatientService;



public class PatientFormAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1363814790548161206L;

	/**
	 * 
	 */
	private int	pfId = 0;
	private int	formId = 0;
	private int	patientId = 0;
	private int	month = 0;

	private Patient patient = null;
	private PatientForm patientForm = null;
	private List<PatientForm> patientFormList = null;
	private Set<PatientFormBlank> patientFormBlanks = null;
	private PatientFormBlank[] blankList = null;
	
	private PatientService patientService = null;
	private FormService formService = null;
	private PatientFormService patientFormService = null;
	private PatientFormBlankService patientFormBlankService = null;

	
	
	public int getPfId() {
		return pfId;
	}

	public void setPfId(int pfId) {
		this.pfId = pfId;
	}
	
	public int getPatientId() {
		return patientId;
	}

	public void setPatientId(int patientId) {
		this.patientId = patientId;
	}


	public int getFormId() {
		return formId;
	}

	public void setFormId(int formId) {
		this.formId = formId;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}
	
	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}
	
	public List<PatientForm> getPatientFormList() {
		return patientFormList;
	}

	
	public void setPatientFormList(List<PatientForm> patientFormList) {
		this.patientFormList = patientFormList;
	}

	public PatientForm getPatientForm() {
		return patientForm;
	}

	public void setPatientForm(PatientForm patientForm) {
		this.patientForm = patientForm;
	}
	
	
	public Set<PatientFormBlank> getPatientFormBlanks() {
		return patientFormBlanks;
	}

	public void setPatientFormBlanks(Set<PatientFormBlank> patientFormBlanks) {
		this.patientFormBlanks = patientFormBlanks;
	}

	public PatientFormBlank[] getBlankList() {
		return blankList;
	}

	public void setBlankList(PatientFormBlank[] blankList) {
		this.blankList = blankList;
	}

	public PatientFormService getPatientFormService() {
		return patientFormService;
	}
	
	public void setPatientFormService(PatientFormService patientFormService) {
		this.patientFormService = patientFormService;
	}
	
	
	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	

	public PatientFormBlankService getPatientFormBlankService() {
		return patientFormBlankService;
	}

	public void setPatientFormBlankService(
			PatientFormBlankService patientFormBlankService) {
		this.patientFormBlankService = patientFormBlankService;
	}

	public String show() {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String detail(){
		try {
			patientForm = patientFormService.getPatientFormById(pfId);
			if(patientForm != null) {
				patient = patientForm.getPatient();
				
				if(blankList == null || blankList.length < patientFormBlanks.size()) {
					blankList = new PatientFormBlank[patientForm.getPatientFormBlanks().size()];
				}
				
				patientForm.getPatientFormBlanks().toArray(blankList);
				month = patientForm.getMonth();
//				for(int i = 0; i < blankList.length; i++) {
//					System.out.println(blankList[i].getBlank().getBlankId() + " " + blankList[i].getStrValue() + " "+ blankList[i].getIntValue());
//					
//				}
			
				return patientForm.getForm().getFormPath();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	public String list(){
		try {
			Patient patient = patientService.getPatientById(patientId);
			if(patient != null) {
				patientFormList = patientFormService.getPatientForm(patient);
				
				HttpServletRequest request = ServletActionContext.getRequest();
				request.setAttribute("patientFormList", patientFormList);
				request.setAttribute("patientId", patient.getPatientId());
				
				return SUCCESS;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	@SuppressWarnings("unchecked")
	public String add(){
		try {
			System.out.println("form add" + patientId + " " + formId );
			if(formId == 2 && month != 0) {
				formId = 8;
			}
			
			Form form = formService.getFormById(formId);
			patient = patientService.getPatientById(patientId);
			
			patientForm = patientFormService.getPatientForm(patient, form, month);
			
			if(patientForm == null) {
				patientForm = new PatientForm();
				patientForm.setForm(form);
				patientForm.setPatient(patient);
				patientForm.setMonth(month);
				patientForm.setCreateTime(new Timestamp(new Date().getTime()));
			
				patientFormBlanks = patientForm.getPatientFormBlanks();
				
				Set<?> blanks = form.getBlanks();
		
				for(Iterator<?> i = blanks.iterator(); i.hasNext(); ) {
					PatientFormBlank bb = new PatientFormBlank();
					bb.setStrValue("");
					bb.setIntValue(-1);
					bb.setPatientForm(patientForm);
					bb.setBlank((Blank) i.next());
					
					patientFormBlanks.add(bb);
					
				}
			
				patientFormService.save(patientForm);
			} 
			pfId = patientForm.getPfId();
			return SUCCESS;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	@SuppressWarnings("unchecked")
	public String delete(){
		try {
			System.out.println("delete" + pfId);
			
			patientForm = patientFormService.getPatientFormById(pfId);
		
			if(patientForm != null) {
				patientFormBlanks = patientForm.getPatientFormBlanks();	
				
				for(Iterator<PatientFormBlank> i = patientFormBlanks.iterator(); i.hasNext(); ) {
					PatientFormBlank bb = (PatientFormBlank) i.next();
					System.out.println(bb.getBlank().getBlankId());
					patientFormBlankService.deletePatientFormBlank(bb);
				}
				patientFormService.deletePatient(patientForm);
				return SUCCESS;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	@SuppressWarnings("unchecked")
	public String edit(){
		try {
			System.out.println("edit" + pfId);
			
			HttpServletRequest request = ServletActionContext.getRequest ();
			
			patientForm = patientFormService.getPatientFormById(pfId);
			
			
			if(patientForm != null) {
				
				patientFormBlanks = patientForm.getPatientFormBlanks();	
				
				for(Iterator<PatientFormBlank> i = patientFormBlanks.iterator(); i.hasNext(); ) {		
					PatientFormBlank fb = (PatientFormBlank) i.next();
					String key = "b"+fb.getBlank().getBlankId();
					
					String value = request.getParameter(key);
					if(value != null) {
						System.out.print(key +" "+value + " ");
						if(fb.getBlank().getBlankType() == 1) {		//text
							System.out.println("txt");
							fb.setStrValue(value);
						} else {
							System.out.println("int");
							fb.setIntValue(Integer.parseInt(value));			
						}
					} else {
						System.out.println(key +" null");
						fb.setStrValue("");
						fb.setIntValue(-1);
					}
				}
				patientFormService.update(patientForm);
				
				patient = patientForm.getPatient();
				patientId = patient.getPatientId();
				
				return SUCCESS;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return ERROR;
	}
}
